package BLC

import "github.com/boltdb/bolt"

type Iterator struct{
	CurrentHash []byte
	DB *bolt.DB
}

func (bc *BlockChain)NewIterator()*Iterator{
	return &Iterator{bc.Tip,bc.DB}
}

func (iter *Iterator)Next()*Iterator{

	err:=iter.DB.View(func(tx *bolt.Tx)error{

		b:=tx.Bucket([]byte(blockBucket))

		blockByte:=b.Get(iter.CurrentHash)

		if blockByte==nil{
			iter=nil
		}else {
			block := UnSerilizeBlock(blockByte)
			block.Print()
			iter = &Iterator{block.PreviewHash, iter.DB}
		}

		return nil
	})
	if err!=nil{
		panic(err)
	}

	return iter
}